package com.netease.xone.image;

import android.content.Context;
import android.graphics.Bitmap;

/**
 * 图片美化处理：素描、菲林、日系......
 */
public class ImageProcess {
	public static final int TYPE_SKETCH = 1;    // 素描
	public static final int TYPE_FILM = 2;      // 菲林
	public static final int TYPE_JAPAN = 3;     // 日系
	public static final int TYPE_LOMO = 4;      // LOMO
	public static final int TYPE_SPEIA = 5;     // 怀旧
	public static final int TYPE_BLACKWHITE = 6;// 黑白
	public static final int TYPE_WHITING = 7;   // 美白
	public static final int TYPE_ENHANCE = 8;   // 增强
	public static final int TYPE_TILT = 9;      // 移轴

	private final static String TAG = "ImageProcess";

	private static int mWidth;
	private static int mHeight;
	private static int[] mPixels;

	public static Bitmap process(Context context, Bitmap source, int type) {
		if (isIllegal(source))
			return null;

		if (type == 0)
			return source;

		ini(source);

		Bitmap result = null;
		try {
			switch (type) {
			case TYPE_SKETCH:
				mPixels = sketchWithImage(mPixels, mWidth, mHeight);
				break;
			case TYPE_FILM:
				mPixels = filmWithImage(mPixels, mWidth, mHeight);
				break;
			case TYPE_JAPAN:
				mPixels = japanWithImage(mPixels, mWidth, mHeight);
				break;
			case TYPE_LOMO:
				mPixels = lomoWithImage(mPixels, mWidth, mHeight);
				break;
			case TYPE_SPEIA: 
				mPixels = speiaWithImage(mPixels, mWidth, mHeight);
				break;
			case TYPE_BLACKWHITE:
				mPixels = blackWhiteWithImage(mPixels, mWidth, mHeight);
				break;
			case TYPE_WHITING:
				mPixels = whiteningWithImage(mPixels, mWidth, mHeight);
				break;
			case TYPE_ENHANCE:
				mPixels = enhanceWithImage(mPixels, mWidth, mHeight);
				break;
			case TYPE_TILT:
				mPixels = tiltShiftWithImage(mPixels, mWidth, mHeight);
				break;
			}
		} catch (Exception e) {
			mPixels = null;
		}
		if (mPixels == null)
			return null;
		result = Bitmap.createBitmap(mPixels, mWidth, mHeight,
				Bitmap.Config.ARGB_4444);
		mPixels = null;
		return result;
	}

	private static void ini(Bitmap source) {
		mWidth = source.getWidth();
		mHeight = source.getHeight();
		mPixels = null;
		mPixels = new int[mWidth * mHeight];
		source.getPixels(mPixels, 0, mWidth, 0, 0, mWidth, mHeight);
	}

	private native static int[] sketchWithImage(int[] mData, int mWidth,
			int mHeight);

	private native static int[] speiaWithImage(int[] mData, int mWidth,
			int mHeight);

	private native static int[] lomoWithImage(int[] mData, int mWidth,
			int mHeight);

	private native static int[] filmWithImage(int[] mData, int mWidth,
			int mHeight);

	private native static int[] japanWithImage(int[] mData, int mWidth,
			int mHeight);

	private native static int[] blackWhiteWithImage(int[] mData, int mWidth,
			int mHeight);

	private native static int[] whiteningWithImage(int[] mData, int mWidth,
			int mHeight);

	private native static int[] enhanceWithImage(int[] mData, int mWidth,
			int mHeight);

	private native static int[] tiltShiftWithImage(int[] mData, int mWidth,
			int mHeight);

	/**
	 * 判断图片是否合法
	 * 
	 * @param source
	 * @return
	 */
	private static boolean isIllegal(Bitmap source) {
		if (source == null || source.isRecycled())
			return true;
		return false;
	}

	static {
		System.loadLibrary("image_process");
	}

	public static void clear() {
		mPixels = null;
	}
}
